adbox.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use client";
  2. import { AdItem } from "@/api/customservice";
  3. import { useRouter } from "@/i18n/routing";
  4. import { server } from "@/utils/client";
  5. import { useRequest } from "ahooks";
  6. import React from "react";
  7. import { Autoplay, Pagination } from "swiper/modules";
  8. import { Swiper, SwiperSlide } from "swiper/react";
  9. export const getLoginAdApi = async () => {
  10. return server
  11. .request<AdItem[]>({
  12. url: "/v1/api/front/activity_promotion_guests",
  13. method: "POST",
  14. })
  15. .then((res) => {
  16. return res.data;
  17. })
  18. .catch((err) => {
  19. return [];
  20. });
  21. };
  22. interface AdboxProps {
  23. // data: AdItem[];
  24. }
  25. const Adbox: React.FC<AdboxProps> = () => {
  26. const router = useRouter();
  27. const { data } = useRequest(getLoginAdApi);
  28. const doClick = async (data: AdItem) => {
  29. switch (data.action_type) {
  30. case 2:
  31. window.open(data.action_params, "_blank");
  32. break;
  33. case 3:
  34. let path = data.action_params;
  35. if (path) router.push(path);
  36. break;
  37. case 5:
  38. data?.action_params ? await eval(data?.action_params || "") : "";
  39. break;
  40. default:
  41. break;
  42. }
  43. };
  44. return (
  45. <div className="p-[.1rem]">
  46. <Swiper
  47. spaceBetween={10}
  48. autoplay
  49. modules={[Pagination, Autoplay]}
  50. pagination={{ clickable: true }}
  51. >
  52. {!!data?.length &&
  53. data.map((item) => {
  54. return (
  55. <SwiperSlide key={item.id} onClick={() => doClick(item)}>
  56. <img src={item.content} alt="" />
  57. </SwiperSlide>
  58. );
  59. })}
  60. </Swiper>
  61. </div>
  62. );
  63. };
  64. export default Adbox;